package it.secse.idmadaption.engine;



import java.util.Calendar;
import java.util.TimeZone;

import javax.xml.namespace.QName;

import it.secse.google.wrapper.CheckScheduleDocument;
import it.secse.google.wrapper.CheckScheduleResponseDocument;
import it.secse.google.wrapper.RequestMeetingDocument;
import it.secse.idmadaption.parser.ConfigParser;
import it.secse.idmadaption.parser.ParserIF;
import it.secse.idmadaption.plugin.AbstractPlugin;
import it.secse.idmadaption.plugin.PluginFactory;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;

public class IDMAdapter implements IDMAdapterIF {

	/**
	 * @param args
	 */
	private static ParserIF parser = null;
	
	public static void main(String[] args) {
		IDMAdapter adapt = new IDMAdapter();
		try {
			CheckScheduleDocument request = CheckScheduleDocument.Factory.newInstance();
			CheckScheduleDocument.CheckSchedule  param = CheckScheduleDocument.CheckSchedule.Factory.newInstance();
			param.setStrMailbox("gianluca.ripa@gmail.com");

			// SISTEMARE ORE +2
			Calendar arg0 = Calendar.getInstance();	
			arg0.add(Calendar.HOUR_OF_DAY, 2);
			TimeZone zone = arg0.getTimeZone();
			param.setStart(arg0);
			System.out.println(arg0.getTimeZone().toString());
			Calendar arg1 = Calendar.getInstance();
			arg1.add(Calendar.HOUR_OF_DAY, 4);
			param.setEnd(arg1);			
			System.out.println(arg1.getTime().toLocaleString());
			request.setCheckSchedule(param);
		
			// ciao giampi

//			adapt.receive("checkSchedule", "https://10.10.2.12:8443/axis2/services/ut-over-https", request, "gianluca.ripa@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");

//			adapt.receive("checkSchedule", "https://127.0.0.1:8443/axis2/services/ut-over-https", request, "gianluca.ripa@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
//pc fabioo
//			adapt.receive("checkSchedule", "https://10.10.2.12:8443/axis2/services/ut-over-https", request, "gianluca.ripa@gmail.com", "http://" + "10.10.2.12" + ":8015/clientcallback");
			
			CheckScheduleResponseDocument response = (CheckScheduleResponseDocument) adapt.receive("checkSchedule", "https://127.0.0.1:8443/axis2/services/GoogleWrapper", request, "gianluca.ripa@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
//			CheckScheduleResponseDocument response = (CheckScheduleResponseDocument) adapt.receive("checkSchedule", "https://10.10.2.12:8443/axis2/services/ut-over-https", request, "gianluca.ripa@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
			
			boolean bolResponse = response.getCheckScheduleResponse().getReturn();
			
			if (bolResponse == false) {
				
				RequestMeetingDocument document = RequestMeetingDocument.Factory.newInstance();
				RequestMeetingDocument.RequestMeeting meeting = RequestMeetingDocument.RequestMeeting.Factory.newInstance();
				meeting.setMailbox("gianluca.ripa@gmail.com");
				meeting.setRequestSubject("Travel");
				meeting.setDescription("Travel on Rome");
				meeting.setLocation("On the Road");
				Calendar start = Calendar.getInstance();
				start.add(Calendar.HOUR_OF_DAY, 2);
				meeting.setStart(start);
				Calendar end = Calendar.getInstance();
				end.add(Calendar.HOUR_OF_DAY, 4);
				meeting.setEnd(end);
				document.setRequestMeeting(meeting);
				
//				adapt.receive("requestMeeting", "https://127.0.0.1:8443/axis2/services/ut-over-https", document, "gianluca.ripa@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
				
			    adapt.receive("requestMeeting", "https://127.0.0.1:8443/axis2/services/GoogleWrapper", document, "gianluca.ripa@gmail.com", "http://" + "127.0.0.1" + ":8015/clientcallback");
				
				
				
			}

	        System.out.println(response.getCheckScheduleResponse().getReturn());
	    		
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	
	public Object receive(String operationName, String realEndPoint,
			Object clientRequest, String delegator, String callback)
			throws AxisFault {
		    
		    parser = new ConfigParser(operationName);
//			System.out.println(parser.getPlugin());
			
			AbstractPlugin plug = PluginFactory.newInstance(operationName);
			System.out.println("OK FACTORY");
			Object response = plug.manage(operationName, realEndPoint, clientRequest, delegator, callback);
		
		return response;

	}
	
	public static ParserIF getParser() {
		return parser;
	}

}
